package com.dc.designPattern._17_mediator.impl;

import com.google.common.collect.Maps;

import java.util.Map;

/**
 * Description: 中介者模式_具体的中介者
 * @author: gdc
 * @date: 2021/6/2
 * @version 1.0
 */
public class ConcreteMediator implements Mediator {

    private Map<String, Colleague> colleagueMap;
    private Map<Class, String> interMap;

    public ConcreteMediator() {
        this.colleagueMap = Maps.newHashMap();
        this.interMap = Maps.newHashMap();
    }

    @Override
    public void register(String colleagueName, Colleague colleague) {
        colleagueMap.put(colleagueName, colleague);

        if (colleague instanceof Alarm) {
            interMap.put(Alarm.class, colleagueName);
        } else if (colleague instanceof CoffeeMachine) {
            interMap.put(CoffeeMachine.class, colleagueName);
        } else if (colleague instanceof Tv) {
            interMap.put(Tv.class, colleagueName);
        } else if (colleague instanceof Curtains) {
            interMap.put(Curtains.class, colleagueName);
        }
    }

    @Override
    public void getMessage(int stateChange, String colleagueName) {
        if (colleagueMap.get(colleagueName) instanceof Alarm) {                     // 处理闹钟发出的消息
            if (0 == stateChange) {
                ((CoffeeMachine)(colleagueMap.get(interMap.get(CoffeeMachine.class)))).startCoffee();
                ((Tv)(colleagueMap.get(interMap.get(Tv.class)))).startTv();
                ((Curtains)(colleagueMap.get(interMap.get(Curtains.class)))).close();
            } else if (1 == stateChange) {
                ((Tv)(colleagueMap.get(interMap.get(Tv.class)))).closeTv();
                ((Curtains)(colleagueMap.get(interMap.get(Curtains.class)))).open();
            }
        } else if (colleagueMap.get(colleagueName) instanceof CoffeeMachine) {      // 处理咖啡机发出的消息
            ((Curtains)(colleagueMap.get(interMap.get(Curtains.class)))).open();
        } else if (colleagueMap.get(colleagueName) instanceof Tv) {                 // 处理电视发出的消息
            if (0 == stateChange) {
                ((Curtains)(colleagueMap.get(interMap.get(Curtains.class)))).close();
            } else if (1 == stateChange) {
                ((Curtains)(colleagueMap.get(interMap.get(Curtains.class)))).open();
            }
        } else if (colleagueMap.get(colleagueName) instanceof Curtains) {           // 处理窗帘发出的消息

        }
    }

    @Override
    public void sendMessage() {

    }
}
